//
//  InputViewController.m
//  Juejue
//
//  Created by 吴军虎吴军虎 on 16/3/22.
//  Copyright © 2016年 lanjue. All rights reserved.
//

#import "InputViewController.h"

@interface InputViewController ()<UITextFieldDelegate>

@property (nonatomic,retain) UIView *myheaderView;
@property (nonatomic,retain) UILabel *haolabel;
@property(nonatomic,retain)UITextField *chinaLabel;
@property(nonatomic,retain)UITextField *yanlabel;
@property (nonatomic,retain) UIButton *yanzhnegmaV;
@property (assign, nonatomic) NSInteger timeNum;
@property (strong, nonatomic) NSTimer *getTime;


@end

@implementation InputViewController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden=YES;
    self.title=@"修改手机密码";
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden=NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.view.backgroundColor=LV;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    
    
    //显示文字
    UILabel *label=[[UILabel alloc]init];
    label.text=@"当前手机号";
    label.textColor=[UIColor grayColor];
    label.alpha=0.6;
    label.textAlignment=NSTextAlignmentCenter;
    label.font=[UIFont systemFontOfSize:16];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make){
    
        make.top.equalTo(self.view.mas_top).offset(108/2 *AUTO_WIDTH);
        make.left.equalTo(self.view).offset(60/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width-60 *AUTO_WIDTH, 45 *AUTO_HEIGHT));
    }];
    
    
    
    //手机号码
    _haolabel=[[UILabel alloc]init];
    _haolabel.text=@"180xxxx4730";
    _haolabel.textAlignment=NSTextAlignmentCenter;
    _haolabel.font=[UIFont systemFontOfSize:20];
    _haolabel.alpha=0.5;
    [self.view addSubview:_haolabel];
    [_haolabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(label.mas_bottom).offset(10/2 *AUTO_HEIGHT);
        make.left.equalTo(self.view).offset(60/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width-60 *AUTO_WIDTH, 25 *AUTO_HEIGHT));
    }];
    
    
    
    //第一个框
    UIView *oneView=[[UIView alloc]init];
    oneView.backgroundColor=[UIColor whiteColor];
    oneView.layer.masksToBounds=YES;
    oneView.layer.cornerRadius=5;
    [self.view addSubview:oneView];
    [oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(label.mas_bottom).offset(125/2 *AUTO_WIDTH);
        make.left.equalTo(self.view).offset(44/2 *AUTO_WIDTH);
        make.right.equalTo(self.view).offset(-44/2 *AUTO_WIDTH);
        make.height.mas_equalTo(84/2 *AUTO_WIDTH);
    }];

    
    //手机
    UIImageView *shouji=[[UIImageView alloc]init];
    shouji.image=[UIImage imageNamed:@"shou"];
    [self.view addSubview:shouji];
    [shouji mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(oneView).offset(18/2 *AUTO_WIDTH);
        make.left.equalTo(oneView).offset(36/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(13 *AUTO_WIDTH, 23 *AUTO_WIDTH));
    }];
    
    //竖线
    UILabel *shulabel=[[UILabel alloc]init];
    shulabel.alpha=0.5;
    shulabel.backgroundColor=[UIColor grayColor];
    [self.view addSubview:shulabel];
    [shulabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(oneView).offset(-8.00*AUTO_WIDTH);
        make.top.equalTo(oneView).offset(8.00*AUTO_WIDTH);
        make.left.equalTo(shouji).offset(10 + 20.00 *AUTO_WIDTH);
        make.width.mas_equalTo(1);
    }];
    
    
    //输入框
    _chinaLabel=[[UITextField alloc]init];
    _chinaLabel.delegate=self;
    _chinaLabel.tag=170;
    _chinaLabel.tintColor=GH;
    _chinaLabel.placeholder=@" 137-2333-2222";
    _chinaLabel.textColor=[UIColor blackColor];
    _chinaLabel.clearButtonMode=UITextFieldViewModeWhileEditing;
    _chinaLabel.textAlignment=NSTextAlignmentLeft;
    _chinaLabel.adjustsFontSizeToFitWidth=YES;
    _chinaLabel.keyboardType=UIKeyboardTypeNumberPad;
    _chinaLabel.font=[UIFont systemFontOfSize:12];
    [self.view addSubview:_chinaLabel];
    [_chinaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(oneView).offset(16/2 *AUTO_WIDTH);
        make.left.equalTo(shulabel.mas_left).offset(10*AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(260 *AUTO_WIDTH, 26*AUTO_WIDTH));
    }];
    
  

    //第三个框
    UIView *threeView=[[UIView alloc]init];
    threeView.backgroundColor=[UIColor whiteColor];
    threeView.layer.masksToBounds=YES;
    threeView.layer.cornerRadius=5;
    [self.view addSubview:threeView];
    [threeView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(oneView.mas_bottom).offset(48/2 *AUTO_WIDTH);
        make.left.equalTo(self.view).offset(44/2 *AUTO_WIDTH);
        make.right.equalTo(self.view).offset(-44/2 *AUTO_WIDTH);
        make.height.mas_equalTo(84/2 *AUTO_WIDTH);
    }];
    
    
    //手机2
    UIImageView *shouImage=[[UIImageView alloc]init];
    shouImage.image=[UIImage imageNamed:@"shouji"];
    [self.view addSubview:shouImage];
    [shouImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(threeView).offset(18/2 *AUTO_WIDTH);
        make.left.equalTo(threeView).offset(36/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(13 *AUTO_WIDTH, 23 *AUTO_WIDTH));
    }];
    
    
    //竖线
    UILabel *shulabel2=[[UILabel alloc]init];
    shulabel2.alpha=0.5;
    shulabel2.backgroundColor=[UIColor grayColor];
    [self.view addSubview:shulabel2];
    [shulabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(threeView).offset(-8.00*AUTO_WIDTH);
        make.top.equalTo(threeView).offset(8.00*AUTO_WIDTH);
        make.left.equalTo(shouImage).offset(10 + 20.00 *AUTO_WIDTH);
        make.width.mas_equalTo(1);
    }];
    
    
    //验证码
    _yanlabel=[[UITextField alloc]init];
    _yanlabel.delegate=self;
    _yanlabel.placeholder=@" 验证码";
    _yanlabel.tintColor=GH;
    _yanlabel.textColor=[UIColor blackColor];
    _yanlabel.textAlignment=NSTextAlignmentLeft;
    _yanlabel.adjustsFontSizeToFitWidth=YES;
    _yanlabel.keyboardType=UIKeyboardTypeNumberPad;
    _yanlabel.font=[UIFont systemFontOfSize:12];
    [self.view addSubview:_yanlabel];
    [_yanlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(threeView).offset(10/2 *AUTO_WIDTH);
        make.left.equalTo(shulabel2.mas_left).offset(10*AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(145 *AUTO_WIDTH, 32*AUTO_WIDTH));
    }];
    
    
    //获取验证码
    _yanzhnegmaV= [UIButton buttonWithType:UIButtonTypeCustom];
    _yanzhnegmaV.backgroundColor = [UIColor whiteColor];
    [_yanzhnegmaV addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
    [_yanzhnegmaV setTitleColor:GH forState:UIControlStateNormal];
    [_yanzhnegmaV setTitle:@"获取验证码" forState:UIControlStateNormal];
    _yanzhnegmaV.titleLabel.font = [UIFont systemFontOfSize:9.00 *AUTO_WIDTH];
    _yanzhnegmaV.layer.masksToBounds = YES;
    _yanzhnegmaV.layer.cornerRadius = 20.00*AUTO_WIDTH/2;
    _yanzhnegmaV.layer.borderColor = [GH CGColor];
    _yanzhnegmaV.layer.borderWidth = 1.0;
    [self.view addSubview:_yanzhnegmaV];
    [_yanzhnegmaV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(threeView).offset(24.00 * AUTO_WIDTH/2);
        make.right.equalTo(threeView).offset(-15.00*AUTO_WIDTH/2);
        make.bottom.equalTo(threeView).offset(-22*AUTO_WIDTH/2);
        make.width.mas_equalTo(107.00 *AUTO_WIDTH/2);
        
    }];


    
    //确定修改手机号码
    UIButton *Dview=[[UIButton alloc]init];
    Dview.backgroundColor=GH;
    Dview.layer.cornerRadius=5;
    [Dview setTitle:@"确定修改手机号码" forState:UIControlStateNormal];
    [Dview setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    Dview.titleLabel.font=[UIFont fontWithName:@"Arial" size:LOGINFONT *TEXTWEIGHT];
    [Dview addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Dview];
    [Dview mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.top.equalTo(threeView.mas_bottom).offset(150/2 *AUTO_WIDTH);
        make.left.equalTo(self.view.mas_left).offset(44/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width-88/2 *AUTO_WIDTH, 78/2 *AUTO_WIDTH));
    }];
    
   
}


#pragma mark -设置textField的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField ==self.chinaLabel) {
        
        if (textField.tag == 170) {
            NSString *text = [textField text];
            NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
            string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
            if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
                return NO;
            }
            text = [text stringByReplacingCharactersInRange:range withString:string];
            text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
            NSString *newString = @"";
            while (text.length > 0) {
                if (newString.length == 0) {
                    NSString *subString = [text substringToIndex:MIN(text.length, 3)];
                    newString = [newString stringByAppendingString:subString];
                    if (subString.length == 3) {
                        newString = [newString stringByAppendingString:@"-"];
                    }
                    text = [text substringFromIndex:MIN(text.length, 3)];
                } else {
                    NSString *subString = [text substringToIndex:MIN(text.length, 4)];
                    newString = [newString stringByAppendingString:subString];
                    if (subString.length == 4) {
                        newString = [newString stringByAppendingString:@"-"];
                    }
                    text = [text substringFromIndex:MIN(text.length, 4)];
                }
            }
            newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
            
            if (newString.length >= 15) {
                return NO;
            }
            
            [textField setText:newString];
            return NO;
            
        } else {
            
            NSString *text = [textField text];
            NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789_QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"];
            if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
                return NO;
            }
            if (text.length >= 25) {
                return NO;
            }
            text = [text stringByReplacingCharactersInRange:range withString:string];
            [textField setText:text];
            return NO;
            
        }
        
        
    }if (textField == self.yanlabel) {
        if (range.location ==4) {
            
            return NO;
        }else if (range.location ==8){
            
            
            
            NSMutableString *str=[[NSMutableString alloc]initWithString:textField.text];
            
            if (range.location != NSNotFound) {
                
            }else{
                
                
                textField.text=str;
            }
            return YES;
        }else if (range.location ==9){
            
            NSMutableString *str=[[NSMutableString alloc]initWithString:textField.text];
            NSString *str1;
            str1=[str stringByReplacingOccurrencesOfString:@"-" withString:@""];
            textField.text=str1;
            return YES;
        }else {
            
            return YES;
        }
        
        
    }
    
    return NO;
    
}

-(void) sendMessage:(UIButton *) btn
{
    if (_chinaLabel.text.length !=11) {
        
        _timeNum=60;
        _getTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
        _yanzhnegmaV.enabled=NO;
        [_yanzhnegmaV setTitle:@"60S" forState:UIControlStateDisabled];
    }
}


//timerFired
-(void) timerFired
{
    if (_timeNum >0) {
        _timeNum--;
        _yanzhnegmaV.enabled=NO;
        [_yanzhnegmaV setTitle:[NSString stringWithFormat:@"%ldS",(long)_timeNum] forState:UIControlStateDisabled];
    }else{
        
        if ([_getTime isValid]) {
            [_getTime invalidate];
            _getTime =nil;
        }
        _yanzhnegmaV.enabled=YES;
        [_yanzhnegmaV setTitle:@"重新获取" forState:UIControlStateNormal];
    }
}

//点击确定修改手机号码
-(void) btnClick:(UIButton *) btn
{
    NSLog(@"按钮被点击了");
}

//返回上一页
-(void)Back:(UIButton *)goback{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}




@end
